/*
 * Copyright 2011 Clean GWT
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.cleangwt.showcase.client.page;

import com.cleangwt.cell.client.SimpleCell;
import com.cleangwt.json.client.SimpleObject;
import com.cleangwt.page.client.PageContext.CodeSplit;
import com.cleangwt.page.client.PageContext.Page;
import com.cleangwt.page.client.PageContext.Prefetch;
import com.cleangwt.showcase.client.component.ShowcasePage;
import com.cleangwt.showcase.client.resource.DataSource;
import com.cleangwt.showcase.client.resource.PageGroups;
import com.cleangwt.user.client.ui.Lay;
import com.cleangwt.view.client.SimpleTreeViewModel;
import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.user.cellview.client.CellTree;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.view.client.DefaultSelectionEventManager;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.MultiSelectionModel;
import com.google.gwt.view.client.SelectionChangeEvent;

/**
 * @author Jess
 * @version 1.0
 * @since 1.0
 */
@Prefetch(PageGroups.RICH_WIDGET)
@CodeSplit
@Page
public class CellTreeDemo extends ShowcasePage {

  private class UserCell extends AbstractCell<SimpleObject> {

    @Override
    public void render(com.google.gwt.cell.client.Cell.Context context, SimpleObject value,
        SafeHtmlBuilder sb) {
      String checked = model.getSelectionModel().isSelected(value) ? "checked" : "";
      sb.appendHtmlConstant("<label>");
      sb.appendHtmlConstant("<input type='checkbox' " + checked + "/>");
      sb.appendEscaped((String) value.get("name"));
      sb.appendHtmlConstant("</label>");
    }
  }

  SimpleTreeViewModel model = new SimpleTreeViewModel(new ListDataProvider<SimpleObject>(DataSource
      .getDepartments()));

  Label lblSelection = new Label();

  public CellTreeDemo() {
    super("The Data Tree", "To present structural data in tree.", "CellTreeDemo.java");
    model.addNode(new SimpleCell("name"), "units");
    model.addNode(new SimpleCell("name"), "users");
    model.addNode(new UserCell());
    model.setSelectionModel(new MultiSelectionModel<SimpleObject>());
    model.setSelectionManager(DefaultSelectionEventManager.<SimpleObject> createCheckboxManager());
    CellTree cellTree = new CellTree(model, null);
    setContentWidget(Lay.hStretch(Lay.ver(cellTree, lblSelection)));

    model.getSelectionModel().addSelectionChangeHandler(new SelectionChangeEvent.Handler() {

      @Override
      public void onSelectionChange(SelectionChangeEvent event) {
        StringBuilder sb = new StringBuilder();
        for (SimpleObject s : ((MultiSelectionModel<SimpleObject>) model.getSelectionModel())
            .getSelectedSet()) {
          sb.append(s.get("name")).append(",");
        }
        if (sb.length() == 0) {
          lblSelection.setText("");
        } else {
          lblSelection.setText(sb.substring(0, sb.length() - 1));
        }
      }
    });
  }

}
